package org.example.dao;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

@Configuration
@PropertySource("classpath:/database.properties")
@MapperScan(basePackages = "org.example.dao")
public class DBConfig {
    @Bean
    public DataSource dataSource(@Value("${db-driver}") String driver,
                                 @Value("${db-url}") String url,
                                 @Value("${db-user}") String username,
                                 @Value("${db-password}") String password){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setMaxActive(10);          //最大活动连接数
        dataSource.setMinIdle(2);             //最小空间连接数
        dataSource.setQueryTimeout(10);        //最长查询时间
        return dataSource;
    }
}
